﻿package mortal.game.view.issm
{
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class IssmController extends Controller
    {
        private var blackBg:Sprite;

        public function IssmController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (!_view)
            {
                _view = new IssmModule();
                _view.addEventListener(WindowEvent.SHOW, this.onViewShow);
                _view.addEventListener(WindowEvent.CLOSE, this.onViewHide);
            }
            return _view;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.IssmUpdate, this.sIssmHandler);
            NetDispatcher.addCmdListener(ServerCommand.IssmResult, this.issmResult);
            return;
        }// end function

        private function showModule(param1:Object = null) : void
        {
            if (Cache.instance.role.playerInfo.issm == EIssm._EIssmNotPass)
            {
                GameManager.instance.popupModuleType(ModuleType.Issm);
            }
            return;
        }// end function

        private function issmResult(param1:Object) : void
        {
            var _loc_2:* = int(param1);
            if (_loc_2 == EIssm._EIssmAdult)
            {
                this.hideView();
                this.blackBgVisible = false;
                MsgManager.showRollTipsMsg(Language.getString(11806));
            }
            else if (_loc_2 == EIssm._EIssmUnAdult)
            {
                this.hideView();
                MsgManager.showRollTipsMsg(Language.getString(11807));
            }
            return;
        }// end function

        private function sIssmHandler(param1:Object) : void
        {
            var onSelct:Function;
            var e:* = param1;
            onSelct = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    if (ParamsConst.instance.proctecUrl)
                    {
                        BrowerUtil.getUrl(ParamsConst.instance.proctecUrl);
                    }
                    else
                    {
                        GameController.issm.view.show();
                        if (blackBgVisible)
                        {
                            LayerManager.highestLayer.addChildAt(GameController.issm.view as DisplayObject, 1);
                        }
                    }
                }
                return;
            }// end function
            ;
            var sIssm:* = e as SIssmMsg;
            Log.debug("sIssm.onlineHour " + sIssm.onlineHour);
            var str:String;
            if (sIssm.onlineHour == 0)
            {
                return MsgManager.showRollTipsMsg(Language.getString(11808));
            }
            if (sIssm.onlineHour == 1)
            {
                if (sIssm.issm == EIssm._EIssmUnAdult)
                {
                    str = Language.getStringByParam(11809, sIssm.onlineHour.toString());
                }
                else
                {
                    str = Language.getStringByParam(11810, sIssm.onlineHour.toString());
                }
            }
            else if (sIssm.onlineHour == 2)
            {
                if (sIssm.issm == EIssm._EIssmUnAdult)
                {
                    str = Language.getStringByParam(11811, sIssm.onlineHour.toString());
                }
                else
                {
                    str = Language.getStringByParam(11812, sIssm.onlineHour.toString());
                }
            }
            else if (sIssm.onlineHour == 7)
            {
                str = Language.getString(11813);
            }
            else if (sIssm.onlineHour == 9)
            {
                str = Language.getString(11814);
                this.blackBgVisible = true;
                if (sIssm.issm == EIssm._EIssmUnAdult)
                {
                    return;
                }
            }
            Alert.buttonWidth = 65;
            Alert.okLabel = Language.getString(11815);
            Alert.calcelLabel = Language.getString(11816);
            var alertWindow:* = Alert.show(str, null, Alert.OK | Alert.CANCEL, null, onSelct) as GAlertWin;
            if (sIssm.issm == EIssm._EIssmUnAdult)
            {
                alertWindow.getButtonByFlag(Alert.OK).enabled = false;
            }
            if (this.blackBgVisible)
            {
            }
            return;
        }// end function

        private function hideView() : void
        {
            var _loc_1:IssmModule = null;
            if (view && !view.isHide)
            {
                view.hide();
            }
            if (this.blackBgVisible == true)
            {
                _loc_1 = view as IssmModule;
                if (_loc_1 && _loc_1.parent)
                {
                    _loc_1.parent.removeChild(_loc_1);
                }
            }
            return;
        }// end function

        private function onViewShow(event:WindowEvent) : void
        {
            Dispatcher.addEventListener(EventName.IssmCommit, this.ismmCommit);
            return;
        }// end function

        private function ismmCommit(event:DataEvent) : void
        {
            GameProxy.player.commitIssm(event.data.name, event.data.card);
            return;
        }// end function

        private function onViewHide(event:WindowEvent) : void
        {
            Dispatcher.removeEventListener(EventName.IssmCommit, this.ismmCommit);
            return;
        }// end function

        private function set blackBgVisible(param1:Boolean) : void
        {
            if (param1)
            {
                if (this.blackBg == null)
                {
                    this.blackBg = new Sprite();
                    LayerManager.highestLayer.addChildAt(this.blackBg, 0);
                    this.drawBlackBg(this.blackBg);
                }
                this.blackBg.visible = true;
                Alert.setStage(LayerManager.highestLayer);
                MsgManager.msgRollTipsImpl.canvas = LayerManager.highestLayer;
                Global.stage.addEventListener(Event.RESIZE, this.stageResize);
            }
            else
            {
                if (this.blackBg)
                {
                    this.blackBg.graphics.clear();
                    if (this.blackBg.parent)
                    {
                        this.blackBg.parent.removeChild(this.blackBg);
                    }
                    this.blackBg = null;
                }
                Alert.setStage(LayerManager.alertLayer);
                MsgManager.msgRollTipsImpl.canvas = LayerManager.msgTipLayer;
                Global.stage.removeEventListener(Event.RESIZE, this.stageResize);
            }
            return;
        }// end function

        private function get blackBgVisible() : Boolean
        {
            return this.blackBg != null;
        }// end function

        private function drawBlackBg(param1:Sprite) : void
        {
            param1.graphics.clear();
            param1.graphics.beginFill(0, 1);
            param1.graphics.drawRect(0, 0, Global.stage.stageWidth, Global.stage.stageHeight);
            param1.graphics.endFill();
            param1.mouseChildren = false;
            param1.mouseEnabled = false;
            return;
        }// end function

        public function stageResize(event:Event) : void
        {
            if (this.blackBgVisible)
            {
                this.drawBlackBg(this.blackBg);
            }
            return;
        }// end function

    }
}
